package repository

import (
	"errors"

	"github.com/lab-online/internal/user/constant"
	"github.com/lab-online/internal/user/entity"
	"gorm.io/gorm"
)

type Repository struct {
	db *gorm.DB
}
type UserRepository interface {
	DomainRepository
	ExportRepository
}

type DomainRepository interface {
	DeleteUser(entity.UserEntity) error

	SaveUser(entity.UserEntity) (entity.UserEntity, error)
	SaveManyUsers([]entity.UserEntity) ([]entity.UserEntity, error)

	GetUserByUsername(string) (entity.UserEntity, error)
	GetUnscopedUserByUsername(string) (entity.UserEntity, error)
	GetUsers(entity.UserEntity) ([]entity.UserEntity, error)
}

type ExportRepository interface {
	GetUserByUsername(string) (entity.UserEntity, error)
	GetManyUsersByUsername([]string) ([]entity.UserEntity, error)
}

func New(postgres *gorm.DB) UserRepository {
	return &Repository{postgres}
}

func (*Repository) errorHandler(err error) error {
	if errors.Is(err, gorm.ErrRecordNotFound) {
		return constant.DBErrUserNotFound
	}
	return constant.DBErrUnknown
}
